package com.atguigu.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;

public class CodeGenerator {
    /**
     * 数据库名
     */
    public static final String DB_NAME = "jinx_rpo";

    /**
     * 数据库定义URL
     */
    public static final String URL = "jdbc:mysql://124.222.225.192:3306/jinx_rpo?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf8";

    /**
     * 数据库的用户名
     */
    public static final String USERNAME = "root";

    /**
     * 数据库的密码
     */
    public static final String PASSWORD = "YourSuperLongPassword_1234567890!";

    /**
     * 作者
     */
    public static final String AUTHOR = "steve";

    /**
     * 指定输出目录
     */
    public static final String OUTPUT_DIR = "src\\main\\java";

    /**
     * 设置父包名
     */
    public static final String PACKAGE_NAME = "com.atguigu.jinx";

    /**
     * 设置父包模块名
     */
    public static final String MODULE_NAME = "jinx";

    /**
     * 设置entity名称
     */
    public static final String ENTITY = "entity";

    /**
     * 设置体制名称
     */
    public static final String MAPPER = "mapper";
    public static final String SERVICE = "service";
    public static final String SERVICE_IMPL = "service/impl";
    /**
     * 想要生成哪些表(手动输入即可)
     */
//    public static final String[] TABLES = {"candidate", "commission_log","job_task","recharge_order","user","user_job_order","withdraw_request"};
    public static final String[] TABLES = {"recharge_request","transaction_record","user_balance"};


    public static void main(String[] args) {
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(OUTPUT_DIR); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent(PACKAGE_NAME) // 设置父包名
                                .moduleName(MODULE_NAME) // 设置父包模块名
                                .entity(ENTITY)
                                .mapper(MAPPER)
                                .service(SERVICE)
                                .serviceImpl(SERVICE_IMPL)
                )
                .strategyConfig(builder -> {
                    builder.addInclude(TABLES) // 设置需要生成的表名
                            .entityBuilder()
                            .enableLombok() // 启用 Lombok
                            .enableTableFieldAnnotation() // 启用字段注解
                            .controllerBuilder()
                            .enableRestStyle(); // 启用 REST 风格
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}